let change=document.querySelector('.change')
document.querySelector('.xuke').style.display='block'
document.querySelector('.yinsi').style.display='none'
change.onchange=()=>{
    console.log(change.selectedIndex);
    switch (change.selectedIndex) {
        case 0:
            document.querySelector('.xuke').style.display='block'
            document.querySelector('.yinsi').style.display='none'
            break;
        case 1:
            document.querySelector('.xuke').style.display='none'
            document.querySelector('.yinsi').style.display='block'
            break;
        default:
            break;
    }
}
let resignbtn=document.querySelector('button')
let username=document.querySelector('.username')
let password=document.querySelector('.password')
let repassword=document.querySelector('.repassword')
let username_reg=/^\w{3,11}$/
let password_reg=/^\w{6,18}$/
let complete=[0,0,0,0,0]
username.oninput=()=>{
    if(username_reg.test(username.value)){
        let checknameURL=`http://jx.xuzhixiang.top/ap/api/checkname.php?`
        axios.get(checknameURL,{params:{
            username:username.value,
        }}).then(function(resolve){
            if(resolve.data.code==1){
                tip(username.nextElementSibling.nextElementSibling,1)
                complete[2]=1
            }else{
                tip(username.nextElementSibling.nextElementSibling,3)
                complete[2]=0;
            }
        }).catch(function(reject){
            console.log(reject);
        })
    }else{
        tip(username.nextElementSibling.nextElementSibling,2)
        complete[2]=0;
    }
}
password.oninput=()=>{
    if(password_reg.test(password.value)){
        tip(password.nextElementSibling.nextElementSibling,1)
        complete[3]=1
    }else{
        tip(password.nextElementSibling.nextElementSibling,2)
        complete[3]=0
    }
}
repassword.oninput=()=>{
    if(password.value==repassword.value){
        tip(repassword.nextElementSibling.nextElementSibling,1)
        complete[4]=1
    }else{
        tip(repassword.nextElementSibling.nextElementSibling,2)
        complete[4]=0
    }
}
resignbtn.onclick=(evt)=>{
    evt.stopPropagation();
    let agree1=document.querySelector('.agree1')
    let agree2=document.querySelector('.agree2')
    if(agree1.checked){
        complete[0]=1
    }else{
        complete[0]=0
    }
    if(agree2.checked){
        complete[1]=1
    }else{
        complete[1]=0
    }
    if(complete.join('')==11111){
        document.querySelector('.wrap').style.display='block'
        document.querySelector('.wrap div p:first-child span').innerHTML=`${username.value}`
        let resignURL=`http://jx.xuzhixiang.top/ap/api/reg.php`
        axios.get(resignURL,{params:{
            username:username.value,
            password:password.value,
        }}).then(function(resolve){
            let loginURL=`http://jx.xuzhixiang.top/ap/api/login.php`
            axios.get(loginURL,{params:{
                username:username.value,
                password:password.value,
            }}).then(function(resolve){
                let user=resolve.data.data;
                localStorage.setItem('uid',user.id)
                localStorage.setItem('username',user.username)
                localStorage.setItem('password',user.password)
                localStorage.setItem('token',user.token)
            }).catch(function(reject){
                console.log(reject);
            });
            let times=4
            setInterval(() => {
                if (times==0) {
                    location.href='../index.html?'
                }
                document.querySelector('.wrap div p:last-child').innerHTML=`精彩马上开始...(${times})`
                times--;
            }, 1000);        
        }).catch(function(reject){
            console.log(reject);
        });
    }else{
        let tips=document.querySelector('.wrap')
        tips.style.display='block';
        document.querySelector('.wrap div p:first-child').innerHTML=`请阅读并同意，或检查是否输入正确`
        document.querySelector('.wrap div p:last-child').innerHTML=`点击任意处可关闭...`
        document.onclick=()=>{
            tips.style.display='none';
        }
    }
}
function tip(changeElement,i){
    if (i==1) {
        changeElement.style.display='none';
    }else if(i==2){
        changeElement.style.display='block';
        changeElement.style.color='#f00';
        changeElement.style.fontWeight='700';

    }else if(i==3){
        changeElement.innerHTML='用户名已存在';
        changeElement.style.display='block';
        changeElement.style.color='#f00';
        changeElement.style.fontWeight='700';

    }
}